/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 */
package com.apache.order.service.plugins;

import java.util.ArrayList;
import java.util.List;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.cache.util.Validator;
import com.apache.order.entity.TOrder;
import com.apache.order.entity.TOrderDetail;
import com.apache.order.manager.TOrderDetailManager;
import com.apache.order.manager.TOrderManager;

/**
 * description:  保存订单插件
 * @author Hou Dayu 创建时间：2015-8-3  
 */
public class SaveOrderPluginImpl implements PluginConnector {

	private TOrderManager tOrderManager;
	private TOrderDetailManager tOrderDetailManager;

	/**
	 * TODO 保存订单插件实现.  
	 * @see com.dwusoft.api.manager.PluginConnector#execute(com.dwusoft.api.vo.ParamsVo)  
	 */
	public Object execute(ParamsVo vo) throws Exception {
		TOrder orderMain = (TOrder) vo.getParams("order");
		ParamsVo pvo = new ParamsVo();
		pvo.setObj(orderMain);
		String orderId = tOrderManager.saveInfo(pvo);
		if (Validator.isNotNull(orderId)) {
			List<TOrderDetail> list = (List<TOrderDetail>) vo.getParams("product");
			List<TOrderDetail> ods = new ArrayList<TOrderDetail>();
			for (TOrderDetail detail : list) {
				detail.setDetailedId(Validator.generate());
				detail.setOrderId(orderId);
				ods.add(detail);
			}
			pvo.setObj(null);
			pvo.setObjs(ods);
			tOrderDetailManager.saveInfo(pvo);
		}
		return orderId;
	}

	public void settOrderManager(TOrderManager tOrderManager) {
		this.tOrderManager = tOrderManager;
	}

	public void settOrderDetailManager(TOrderDetailManager tOrderDetailManager) {
		this.tOrderDetailManager = tOrderDetailManager;
	}

}
